package interfaz_usuario;

import java.awt.List;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

import eventos.EventInfObjectBar;
import eventos.ObjectBarClickedListener;
import eventos.ObjectBarEvent;



public class ObjectsBarPanel extends JPanel{
	
	private ObjectButton objectList[];
	
	private ArrayList<ObjectBarClickedListener> listListeners;
	private ObjectBarEvent event;
	
	public ObjectsBarPanel(){
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		objectList = new ObjectButton[8];
		for(int i=0;i<5;i++){
			//objectList[i]= new ObjectButton(Constants.IMAGE_BOMBERMAN);
			System.out.println("esto : "+i);
			objectList[i]= new ObjectButton(i);
			add(objectList[i].getButton());
			objectList[i].getButton().addActionListener(new ClickListener(objectList[i].getWorldObjectReference()));
		}
		listListeners= new ArrayList<ObjectBarClickedListener>();
		
	}
	
	public void addListener(ObjectBarClickedListener obj){
		listListeners.add(obj);
	}
	
	private class ClickListener implements ActionListener{
		private int reference;
		
		public ClickListener(int worldObjectReference) {
			reference= worldObjectReference;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			//TODO make a clon from the list
			Iterator it = listListeners.iterator();
			while(it.hasNext()){
				ObjectBarClickedListener obj=(ObjectBarClickedListener) it.next();
			    event = new ObjectBarEvent(new EventInfObjectBar(reference));
				obj.changeObject(event);
			}
			
		}
		
	}

}
